package ru.mcfr.oxygen.framework.operations.table;


import org.apache.log4j.Logger;
import ro.sync.ecss.extensions.commons.table.operations.TableCustomizerConstants;
import ro.sync.ecss.extensions.commons.ui.OKCancelDialog;

import javax.swing.*;
import java.awt.*;

public class TableCustomizerDialog extends OKCancelDialog implements TableCustomizerConstants {

    private static Logger logger = Logger.getLogger(TableCustomizerDialog.class.getName());

    private JSpinner rowsSpinner;

    private JSpinner headerRowsSpinner;

    private JSpinner footerRowsSpinner;

    private JSpinner columnsSpinner;

    private JComboBox defContentCombobx;
    private JComboBox defBorderCombobx;

    public TableCustomizerDialog(JFrame parentFrame) {
        super(parentFrame, "Вставка таблицы", true);

        // The main panel
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        GridBagConstraints gridBagConstr = new GridBagConstraints();
        gridBagConstr.anchor = GridBagConstraints.WEST;
        gridBagConstr.gridy = 0;
        gridBagConstr.gridx = 0;
        gridBagConstr.weightx = 0;
        gridBagConstr.gridwidth = 1;
        gridBagConstr.insets = new Insets(5, 0, 5, 5);
        gridBagConstr.fill = GridBagConstraints.NONE;

        // Table size panel
        JPanel sizePanel = new JPanel(new GridBagLayout());
        sizePanel.setBorder(BorderFactory.createTitledBorder("Настройки таблицы"));

        gridBagConstr.gridy++;
        gridBagConstr.gridx = 0;
        gridBagConstr.weightx = 1;
        gridBagConstr.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstr.gridwidth = 2;
        gridBagConstr.insets = new Insets(5, 0, 5, 0);
        mainPanel.add(sizePanel, gridBagConstr);

        // 'Rows' label
        JLabel rowsLabel = new JLabel("Строки:");
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0;
        c.insets = new Insets(0, 5, 5, 5);
        sizePanel.add(rowsLabel, c);

        // Number of rows text field
        rowsSpinner = new JSpinner();
        rowsSpinner.setName("счетчик строк");
        rowsSpinner.setModel(new SpinnerNumberModel(2, 0, 100, 1));
        c.gridx++;
        c.weightx = 1;
        sizePanel.add(rowsSpinner, c);

        c.gridy++;

        logger.error("1 line");

        //---------------------------------------------

        // 'header Rows' label
        JLabel rowsHeaderLabel = new JLabel("Строк заголовка:");
        c.gridx = 0;
        c.weightx = 0;
        sizePanel.add(rowsHeaderLabel, c);

        // Number of header rows text field
        headerRowsSpinner = new JSpinner();
        headerRowsSpinner.setName("счетчик строк");
        headerRowsSpinner.setModel(new SpinnerNumberModel(2, 0, 100, 1));
        c.gridx++;
        c.weightx = 1;
        sizePanel.add(headerRowsSpinner, c);

        c.gridy++;
        logger.error("2 line");

        //---------------------------------------------

        // 'footer Rows' label
        JLabel rowsFooterLabel = new JLabel("Строк подвала:");
        c.gridx = 0;
        c.weightx = 0;
        sizePanel.add(rowsFooterLabel, c);

        // Number of footer rows text field
        footerRowsSpinner = new JSpinner();
        footerRowsSpinner.setName("счетчик строк");
        footerRowsSpinner.setModel(new SpinnerNumberModel(2, 0, 100, 1));
        c.gridx++;
        c.weightx = 1;
        sizePanel.add(footerRowsSpinner, c);

        c.gridy++;

        //---------------------------------------------

        // 'Columns' label
        JLabel columnsLabel = new JLabel("Колонки:");
        c.gridx = 0;
        c.weightx = 0;
        sizePanel.add(columnsLabel, c);

        // Number of rows text field
        columnsSpinner = new JSpinner();
        columnsSpinner.setName("счетчик колонок");
        columnsSpinner.setModel(new SpinnerNumberModel(2, 0, 100, 1));
        c.gridx++;
        c.weightx = 1;
        sizePanel.add(columnsSpinner, c);

        c.gridy++;

        //---------------------------------------------

        JLabel defaultContentComboLabel = new JLabel("Заполнить ячейки:");
        c.gridx = 0;
        c.weightx = 0;
        sizePanel.add(defaultContentComboLabel, c);

        defContentCombobx = new JComboBox();
        defContentCombobx.setEditable(false);
        c.gridx++;
        c.weightx = 0;
        sizePanel.add(defContentCombobx, c);

        c.gridy++;

        //---------------------------------------------

        JLabel typesOfBorderLabel = new JLabel("Тип границы:");
        c.gridx = 0;
        c.weightx = 0;
        sizePanel.add(typesOfBorderLabel, c);

        defBorderCombobx = new JComboBox();
        defBorderCombobx.setEditable(false);
        c.gridx++;
        c.weightx = 0;
        sizePanel.add(defBorderCombobx, c);

        //---------------------------------------------

        //Add the main panel
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        logger.error("pack the dialog");
        pack();
        setResizable(false);
    }

    public TableInfo showDialog(TableInfo initTableInfo) {

        rowsSpinner.setValue(new Integer(3));
        columnsSpinner.setValue(new Integer(2));
        headerRowsSpinner.setValue(new Integer(0));
        footerRowsSpinner.setValue(new Integer(0));

        TableInfo tableInfo = new TableInfo(0, 0, 0, 0);

        for (String key : initTableInfo.getCellContentSet())
            defContentCombobx.addItem(key);
        defContentCombobx.setSelectedItem("абзац");

        for (String border : initTableInfo.getTypesOfBorder())
            defBorderCombobx.addItem(border);
        defBorderCombobx.setSelectedItem(initTableInfo.getTypesOfBorder()[0]);


        super.setVisible(true);

        if (getResult() == RESULT_OK) {

            tableInfo = new TableInfo(((Integer) rowsSpinner.getValue()).intValue(),
                    ((Integer) columnsSpinner.getValue()).intValue(),
                    ((Integer) headerRowsSpinner.getValue()).intValue(),
                    ((Integer) footerRowsSpinner.getValue()).intValue());

            tableInfo.setContent(initTableInfo.getContent((String) defContentCombobx.getSelectedItem()));
            tableInfo.setTypeOfBorder((String) defBorderCombobx.getSelectedItem());
        } else {
            return null;
        }

        logger.error("finish compile data info to dialog");
        return tableInfo;
    }
}